<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class WorkRelateCourse extends Model
{
    protected $table = "c_work_relate_courses";

    /***
     *  通过关联ID获取工作室id
     */
    public static function getIdsByRelateId($wc_relate_id)
    {
        return self::where([["id", '=', $wc_relate_id]])->first();
    }

    public static function getIdByWorkId($param)
    {
        return self::where([["course_id", '=', $param['course_id']], ["work_id", '=', $param['work_id']]])->first();
    }

    /***
     * @param $param
     * @return mixed
     * 获取工作室已有课程ID
     */
    public static function getCourseIds($param)
    {
        $res =  self::where([['work_id', '=', $param['work_id']]])->pluck("course_id");
        $course = new Course();
//        去除选品后又关闭分享的课程
        foreach($res as $k=>$v){
            $info = $course->where([['id','=',$v]])->first();
            if(empty($info)){
                self::where([['course_id','=',$v]])->delete();
                continue;
            }
            if($info->add_user_id != $param['doctor_id'] && $info->is_share != 1){
                unset($res[$k]);
            }
        }
        return $res;
    }


}
